package cn.itcast.service;

import cn.itcast.dao.OrderSettingMapper;
import cn.itcast.domain.CheckItem;
import cn.itcast.domain.OrderSetting;
import cn.itcast.domain.OrderSettingList;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 保存文件上传的数据
     *
     * @param orderSettings
     */
    @Override
    public void saveExcel(List<OrderSetting> orderSettings) {

        if (orderSettings != null && orderSettings.size() > 0) {
            //遍历orderSettings集合
            for (OrderSetting orderSetting : orderSettings) {
                //先判断 数据库中是否已经有此项数据
                OrderSetting temp = orderSettingMapper.findByDate(orderSetting.getOrderDate());
                if (temp != null) {
                    temp.setNumber(orderSetting.getNumber());
                    orderSettingMapper.update(temp);
                    //修改数据
                } else {
                    //新增数据
                    orderSettingMapper.insert(orderSetting);
                }
            }
        }
    }

    /**
     * 根据日期查询所有的预约设置数据
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //拼接字符串 2019-6-1
        String begin = date + "-1";//2019-6-1
        String end = date + "-31";//2019-6-31
        Map<String, String> map = new HashMap<String, String>();
        map.put("begin", begin);
        map.put("end", end);
        //调用DAO，根据日期范围查询预约设置数据
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);

        //创建一个新的map集合,存放数据,日期,数子,可预约人数
        List<Map> result = new ArrayList<Map>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date", orderSetting.getOrderDate().getDate());//获取日期数字（几号）
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    /**
     * 手动设置可预约人数
     *
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期没有进行预约设置，进行添加操作
            orderSettingMapper.insert(orderSetting);
        }
    }

    /**
     * 预约分页列表展示
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findMapByPage(QueryPageBean queryPageBean) {
        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }

        PageHelper.startPage(currentPage, pageSize);
        Page<OrderSettingList> page = (Page<OrderSettingList>) orderSettingMapper.findMapByPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }
}
